class CreateRolesUsers < ActiveRecord::Migration
  def self.up
    create_table :roles_users,:id => false do |t|
      t.integer :role_id, :user_id, :null => false
    end
    #     Make sure the role migration file was generated first
    Role.create(:name => '超级管理员',
      :description=>'拥有所有权限，包括设置账号角色。')
    Role.create(:name => '网站编辑',
      :description=>'负责添加资料，修改文本。')
    Role.create(:name => '跟单员',
      :description=>'负责管理订单。')
    #     Then, add default admin user
    #     Be sure change the password later or in this migration file
    user = User.new
    user.login = "admin"
    user.email = "iamsuperali@gmail.com"
    user.password = "admin"
    user.password_confirmation = "admin"
    role = Role.find_by_name('超级管理员')
    user.roles << role
    user.save(false)
  end

  def self.down
    drop_table :roles_users
    Role.find_by_name('administrator').destroy
    User.find_by_login('admin').destroy
  end
end
